package com.hao.crm.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hao.crm.domain.Employee;
import com.hao.crm.domain.Menu;
import com.hao.crm.domain.Permission;
import com.hao.crm.page.AjaxResult;
import com.hao.crm.page.PageResult;
import com.hao.crm.query.EmployeeQueryObject;
import com.hao.crm.service.IEmployeeService;
import com.hao.crm.service.IMenuService;
import com.hao.crm.service.IPermissionService;
import com.hao.crm.util.UserContext;

@Controller
public class EmployeeController {
	@Autowired
	IEmployeeService employeeService;
	@Autowired
	IPermissionService permissionService;
	@Autowired
	IMenuService menuService;
	@RequestMapping("/employee")
	public String index() {
		return "employee";
	}

	@RequestMapping("/login")
	@ResponseBody
	public AjaxResult login(String username, String password, HttpServletRequest request) {
		UserContext.set(request);
		AjaxResult result = null;
		Employee e = employeeService.selectBylogin(username, password);
		if (e != null) {
			request.getSession().setAttribute(UserContext.USER_IN_SESSION, e);
			List<Permission> pers = permissionService.selectByEid(e.getId());
			request.getSession().setAttribute(UserContext.PERMISSION_IN_SESSION, pers);
			List<Menu> menus = menuService.queryMenu();
			request.getSession().setAttribute(UserContext.MENU_IN_SESSION, menus);
			result = new AjaxResult(true, "登录成功");
		} else {
			result = new AjaxResult("账号密码错误");
		}
		return result;
	}

	@RequestMapping("/employee_list")
	@ResponseBody
	public PageResult list(EmployeeQueryObject qo) {
		PageResult result = null;
		result = employeeService.selectByCondition(qo);
		return result;
	}
	@RequestMapping("/employee_save")
	@ResponseBody
	public AjaxResult save(Employee emp) {
		AjaxResult result = null;
		try {
			emp.setInputtime(new Date());
			emp.setState(true);
			emp.setAdmin(false);
			emp.setPassword("1");
			int effectCount = employeeService.insert(emp);
			if(effectCount>0) {
				result = new AjaxResult(true, "保存成功");
			}else {
				result = new AjaxResult("保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new AjaxResult("保存异常，请联系管理员");
		}
		return result;
	}
	@RequestMapping("/employee_update")
	@ResponseBody
	public AjaxResult update(Employee emp) {
		AjaxResult result = null;
		try {
			int effectCount = employeeService.update(emp);
			if(effectCount>0) {
				result = new AjaxResult(true, "修改成功");
			}else {
				result = new AjaxResult("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new AjaxResult("修改异常，请联系管理员");
		}
		return result;
	}
	@RequestMapping("/employee_delete")
	@ResponseBody
	public AjaxResult updateState(Long id) {
		AjaxResult result = null;
		try {
			int effectCount = employeeService.updateState(id);
			if(effectCount>0) {
				result = new AjaxResult(true, "操作成功");
			}else {
				result = new AjaxResult("操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = new AjaxResult("异常，请联系管理员");
		}
		return result;
	}

}
